package com.wyq.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience;
import com.wyq.bean.AudienceQInfo;
import com.wyq.bean.Audience_Qtitle;
import com.wyq.service.AudienceQService;
import com.wyq.service.AudienceService;
import com.wyq.service.ShowInfoService;

/**
 * 用户登录
 * @author Fyj
 *
 */
public class AudienceLoginAction extends ActionSupport {
	private String source;//展会渠道
	private Map showInfo;//展会信息
	
	private String audience_name;
	private String audience_email;
	private String audience_mobile;
	
	private String editorText;
	public String errorMsg;
	
	private AudienceService audienceService;
	private ShowInfoService showInfoService;
	private AudienceQService audienceQService;
	
	
	@Override
	public String execute() {
		try{
			//获取展会信息
			String show_id = getShowInfoId();
			
			Map<String,String> options=new HashMap<String, String>();
			options.put("audience_name", audience_name);
			options.put("show_id", show_id);
			if(!"".equals(audience_email)){
				options.put("audience_email", audience_email);
			}
			if(!"".equals(audience_mobile)){
				options.put("audience_mobile", audience_mobile);
			}
			
			if(audienceService.existAudience(options)){
				return updateAudienceInfo(show_id);
				//return "success";
			}else{
				errorMsg="姓名或邮箱或手机号有错误";
				return "login_fail";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "login_fail";
		}
		
	}
	
	public String updateAudienceInfo(String show_id) throws Exception {
		ActionContext context = ActionContext.getContext();   
//		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
//		response.setContentType("text/xml;charset=UTF-8");
//		HttpServletRequest request1 = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
//		response.setContentType("text/xml;charset=UTF-8");
		Map request = (Map)ActionContext.getContext().get("request");
		
		//问卷调查的信息
		List QtitleList = audienceQService.findAllAudience_Qtitle(show_id);
		Set qset = ((Audience_Qtitle)QtitleList.get(0)).getAudienceQs();
		System.out.println(QtitleList);
		
		//用户登录的信息
		//System.out.println("----" + audience_email + "---------" + audience_tel);
		Audience audience = new Audience();
		audience.setAudience_name(audience_name);
		audience.setAudience_Email(audience_email);
		audience.setAudience_mobile(audience_mobile);
		audience.setShow_id(show_id);
		List audienceInfoList = audienceService.getAudienceRegisterInfo(audience);
		Audience audienceInfo = (Audience) audienceInfoList.get(0);
		Set set = audienceInfo.getAudienceQInfos();
		Iterator iterator = set.iterator();
		AudienceQInfo audienceQInfo = null;
		while(iterator.hasNext()){
			audienceQInfo = (AudienceQInfo)iterator.next();
		}
		System.out.println("audienceInfo :" + audienceQInfo.getQInfo());
		//request.put("", value);
		request.put("qTitleList", QtitleList);
		request.put("audienceInfo", audienceInfo);
		request.put("audienceQInfo", audienceQInfo.getQInfo());
		
		return "login_sucess";
			
	}
	
	//获取展会信息
	private String getShowInfoId() throws Exception{
		//获取展会信息
		String show_id = "";
		List<Map> showInfos = showInfoService.getShowInfoByTypeOrUrlChannel(null, source);
		if(null != showInfos && showInfos.size()>0){
			Map domain = (Map)showInfos.get(0);
			showInfo = domain;
			show_id = domain.get("id")!= null?domain.get("id").toString():"";
		}
		return show_id;
	}
	
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		/*System.out.println("ִ����");
		if("".equals(audience_name)||audience_name==null)
		{
			this.addFieldError("audience_name", "������Ϊ��");
			System.out.println("audience_name is null");
		}
		if("".equals(audience_email)&&"".equals(audience_mobile))
		{
			this.addFieldError("neithorNull", "������ֻ���ͬʱΪ��");
			System.out.println("audience_email or audience_mobile is null");
		}*/
	}
	
	public AudienceService getAudienceService() {
		return audienceService;
	}
	public void setAudienceService(AudienceService audienceService) {
		this.audienceService = audienceService;
	}
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	/*public void prepare() throws Exception {
		// TODO Auto-generated method stub
		editorText=as.getIndexPageEditorText(1, true);
	}
	public String getEditorText() {
		return editorText;
	}
	public void setEditorText(String editorText) {
		this.editorText = editorText;
	}*/
	public String getAudience_mobile() {
		return audience_mobile;
	}
	public void setAudience_mobile(String audience_mobile) {
		this.audience_mobile = audience_mobile;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public String getAudience_name() {
		return audience_name;
	}
	public void setAudience_name(String audience_name) {
		this.audience_name = audience_name;
	}
	public String getAudience_email() {
		return audience_email;
	}
	public void setAudience_email(String audience_email) {
		this.audience_email = audience_email;
	}
	public void setShowInfoService(ShowInfoService showInfoService) {
		this.showInfoService = showInfoService;
	}

	public Map getShowInfo() {
		return showInfo;
	}

	public void setAudienceQService(AudienceQService audienceQService) {
		this.audienceQService = audienceQService;
	}


	
}
